package com.zbf.order.web;

import com.alibaba.fastjson.JSON;
import com.zbf.order.entity.MyUser;
import com.zbf.order.mapper.TestOrderMapper;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: LCG
 * 作者: LCG
 * 日期: 2021/3/12  14:25
 * 描述:
 */
@RestController
public class TestOrderController {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private TestOrderMapper testOrderMapper;

    @RequestMapping("addOrder")
    public void addOrder(@RequestBody Map<String,Object> map){
        Long ids = redisTemplate.opsForValue().increment("IDS", 1);
        map.put("id",ids);

        //、存入数据库order
        testOrderMapper.insertOrder(map);
        // 存入消息记录表
        Map<String,Object> mapp=new HashMap<>();
        mapp.put("orderId",ids);
        mapp.put("id",ids);
        mapp.put("state",1); //1表示初始化  2  已发送  3表示发送失败
        mapp.put("orderMs", JSON.toJSONString(map));
        testOrderMapper.insertOrderMesg(mapp);
        //3发送消息;
        try{
            CorrelationData correlationData=new CorrelationData();
            String jsonString = JSON.toJSONString(mapp);
            Message message=new Message(jsonString.getBytes(),null);
            correlationData.setReturnedMessage(message);

            rabbitTemplate.convertAndSend("MY-EXCHANGE","ZHI-LIAN",jsonString,correlationData);
        }catch (Exception e){
            e.printStackTrace();
            //修改消息记录状态为 3
            mapp.put("state",3);
            testOrderMapper.updateOrder(mapp);
        }

    }
    

    /**
     * 作者: LCG
     * 日期: 2021/3/23  20:39
     * 描述: 通过 POST的 RequestBody获取参数
     * @Param []
     * @Return void
     */
    @RequestMapping("testUpload")
    public Object testUpload(@RequestBody Map<String,Object> map){

        System.out.println("=============上传测试============"+map);

        return "{\"zhangsan\":\"123456\"}";

    }



    @RequestMapping("testFormBody")
    public Object testFormBody(HttpServletRequest request,MyUser myUser) throws IOException {

        System.out.println("---header---》"+request.getHeader("token"));

        System.out.println(JSON.toJSONString(myUser));

        Map<String,Object> map=new HashMap<>();

        map.put("fanhi","返回值....");

        String image = myUser.getImage();

        BASE64Decoder base64Decoder=new BASE64Decoder();
        byte[] bytes = base64Decoder.decodeBuffer(image);



        FileOutputStream fileOutputStream=new FileOutputStream(new File("E:/IMAGE/tttt.jpg"));

        fileOutputStream.write(bytes);

        fileOutputStream.close();

        return map;
    }


    /**
     * 作者: LCG
     * 日期: 2021/3/23  23:50
     * 描述: 测试上传文件
     * @Param [file, myUser]
     * @Return void
     */
    @RequestMapping("uploadFile")
    public void testFile(MultipartFile[] file,MyUser myUser){

        System.out.println(file.length);

        System.out.println(file[0].getSize()+"==========="+file[0].getOriginalFilename());

        System.out.println("=====888888======"+myUser);

    }




    @RequestMapping("test003")
    public void test0003(){
        Map<String,Object> mapp=new HashMap<>();
        mapp.put("quantity",1);
        mapp.put("id",1);
        testOrderMapper.updateStock(mapp);

    }

}
